package com.uwlaser.rest;

import com.uwlaser.domain.AntonConfig;
import com.uwlaser.domain.AntonConfigDevice;
import com.uwlaser.domain.AntonConfigParam;
import com.uwlaser.exception.BadRequestException;
import com.uwlaser.service.AntonConfigService;
import com.uwlaser.service.dto.AntonConfigDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.uwlaser.annotation.Log;

import java.util.Set;

/**
 * @author yangwei
 * @create 2022/10/13
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "安东配置")
@RequestMapping("/api/antonConfig")
public class AntonConfigController {
    @Autowired
    private final AntonConfigService antonConfigService;
    private static final String ENTITY_NAME = "antonConfig";

    @GetMapping
    @Log("查询安东配置")
    @ApiOperation("查询安东配置")
    @PreAuthorize("@el.check('antonConfig:list')")
    public ResponseEntity query(AntonConfigParam query, Pageable pageable){
        return new ResponseEntity<>(antonConfigService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增安东配置")
    @ApiOperation("新增安东配置")
    @PreAuthorize("@el.check('antonConfig:add')")
    public ResponseEntity create(@Validated @RequestBody AntonConfigDto resources){
        return new ResponseEntity<>(antonConfigService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改安东配置")
    @ApiOperation("修改安东配置")
    @PreAuthorize("@el.check('antonConfig:edit')")
    public ResponseEntity update(@Validated @RequestBody AntonConfigDto resources){
        if(resources.getId()==null){
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        antonConfigService.updateToId(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @Log("批量删除安东配置")
    @ApiOperation("批量删除安东配置")
    @PreAuthorize("@el.check('antonConfig:del')")
    public ResponseEntity deleteBatch(@RequestBody Set<Integer> ids) {
        antonConfigService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除单条
     * @param id
     * @return
     */
    @DeleteMapping("/deleteOne")
    @Log("删除安东配置")
    @ApiOperation("删除安东配置")
    @PreAuthorize("@el.check('antonConfig:del')")
    public ResponseEntity delete(@RequestBody Integer id) {
        antonConfigService.removeById(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }


}
